<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Object.defineProperties</title>
</head>
<body>
    <script>
        //要求添加 total 属性, 获得班级的总分数， 添加avg 属性，获取班级平均分
        
        var banji = {
            name: 'HTML',
            scores: [
                {
                    name: '张三',
                    score: 90
                },
                {
                    name: '李四',
                    score: 85
                },
                {
                    name: '王五',
                    score: 95
                },
                {
                    name: '赵六',
                    score: 88
                }
            ]
        };
        Object.defineProperties(banji,{
            total:{
                get:function(){
                    var total = 0;//用于累计总分
                    this.scores.forEach(function(item){
                        // console.log(item.score);
                        total += item.score;
                    })
                    return total;
                }
            },
            avg:{
                get:function(){
                    // 总分 / 人数
                    return this.total / this.scores.length;
                }
            }
        })
        console.log( banji.total);
        console.log(banji.avg);
        // banji.avg

        //为 banji 对象定义一个新的属性
        
    </script>
</body>

</html>